<?php

class D_Statement_Mysql 
{	
	private	$dbhost;
	private $username;
	private $password;
	private $dbname;
	private $charset;
	private $pconnect;
	private $link;
	public function __construct($dbhost, $username, $password, $dbname='',$charset='utf8', $pconnect = false)
	{
		
		 $this->dbhost = $dbhost;
		 $this->username = $username;
		 $this->password = $password;
		 $this->dbname = $dbname;
		 $this->charset = $charset;
		 $this->pconnect = $pconnect;
		 //$this->Connect($dbhost, $username, $password, $dbname,$charset, $pconnect);
	}
	public function Connect($dbhost, $username, $password, $dbname='',$charset='utf8', $pconnect = false)
	{
		if($pconnect)
		{
			
			$this->link = mysql_pconnect($dbhost, $username, $password);
			$this->ChangeDb($dbname, $charset);
		}
		else
		{
			$this->link = mysql_connect($dbhost, $username, $password);
		
			$this->ChangeDb($dbname, $charset);
				
		}
	
	}
	public function Query($sql)
	{	
		if(!$this->link)
		{	
			$this->Connect($this->dbhost, $this->username, $this->password, $this->dbname,$this->charset, $this->pconnect);
		}
		$query = mysql_query($sql, $this->link);
		L_Recover::getSql($sql);
	
		if(empty($query))
		{
			trigger_error("mysql query user_error!^_^", E_USER_ERROR);
		
		}
		return $query;
	}
	public function get_num()
	{
		$num_rows = mysql_affected_rows($this->link);
		
		return $num_rows;
	}
	public function Fetch_assocs($sql)
	{	
		$row = $this->ReturnRow($sql,'mysql_fetch_assoc');
		return $row;
	}
	public function Fetch_assoc($sql)
	{	
		$query = $this->Query($sql);
		$row = mysql_fetch_assoc($query);
		return $row;
	}
	public function Fetch_nums($sql)
	{
		$row = $this->ReturnRow($sql,'mysql_fetch_row');
		return $row;
	}
	public function Fetch_num($sql)
	{	
		$query = $this->Query($sql);
		$row = mysql_fetch_row($query);
		return $row;
	}
	public function GetNumRows($sql)
	{	
		$query = $this->Query($sql);
		$num = mysql_num_Rows($query);
		return $num;
	}
	public function ChangeDb($dbname, $charset)
	{
		mysql_select_db($dbname, $this->link);
		mysql_query("set names $charset", $this->link);
	}
	private function ReturnRow($sql,$type)
	{	
		
		$query = $this->Query($sql);
		$num = mysql_num_Rows($query);
		$row = array();
		for($i=0;$i<$num;$i++)
		{
			$row[$i] = $type($query);
		}
		return $row;
	}


}
?>